package com.tudouji.project.farmer.controller;

import java.util.Date;
import java.util.List;

import com.tudouji.common.utils.IdUtils;
import com.tudouji.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.project.farmer.domain.Farmerbatch;
import com.tudouji.project.farmer.service.IFarmerbatchService;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.web.page.TableDataInfo;

/**
 * 种植批次Controller
 * 
 * @author tudouji
 * @date 2021-04-22
 */
@RestController
@RequestMapping("/farmer/farmerbatch")
public class FarmerbatchController extends BaseController
{
    @Autowired
    private IFarmerbatchService farmerbatchService;

    /**
     * 查询种植批次列表
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerbatch:list')")
    @GetMapping("/list")
    public TableDataInfo list(Farmerbatch farmerbatch)
    {
        startPage();
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        List<Farmerbatch> list = null;
        try {
            list = farmerbatchService.selectFarmerbatchList(farmerbatch, tuserId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getDataTable(list);
    }
    /**
     * 查询全部种植批次
     */
    @GetMapping("/getAll")
    public AjaxResult getAll(Farmerbatch farmerbatch)
    {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        List<Farmerbatch> list = null;
        try {
            list = farmerbatchService.selectFarmerbatchList(farmerbatch, tuserId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  AjaxResult.success(list);
    }

    /**
     * 导出种植批次列表
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerbatch:export')")
    @Log(title = "种植批次", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Farmerbatch farmerbatch)
    {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId().toString();
        List<Farmerbatch> list = null;
        try {
            list = farmerbatchService.selectFarmerbatchList(farmerbatch, tuserId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ExcelUtil<Farmerbatch> util = new ExcelUtil<Farmerbatch>(Farmerbatch.class);
        return util.exportExcel(list, "farmerbatch");
    }

    /**
     * 获取种植批次详细信息
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerbatch:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(farmerbatchService.selectFarmerbatchById(guid));
    }

    /**
     * 新增种植批次
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerbatch:add')")
    @Log(title = "种植批次", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Farmerbatch farmerbatch)
    {
        farmerbatch.setGuid(IdUtils.fastSimpleUUID());
        farmerbatch.setCreateTime(new Date());
        return toAjax(farmerbatchService.insertFarmerbatch(farmerbatch));
    }

    /**
     * 修改种植批次
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerbatch:edit')")
    @Log(title = "种植批次", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Farmerbatch farmerbatch)
    {
        return toAjax(farmerbatchService.updateFarmerbatch(farmerbatch));
    }

    /**
     * 删除种植批次
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerbatch:remove')")
    @Log(title = "种植批次", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(farmerbatchService.deleteFarmerbatchByIds(guids));
    }
}
